package com.megurokai.tool;

import android.graphics.Bitmap;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

import com.megurokai.command.CommandPencil;
import com.megurokai.command.CommandFinishPencil;
import com.megurokai.command.CommandStartPencil;
import com.megurokai.paint.PaintManager;
import com.megurokai.paint.Status;

public class ToolPencil implements Tool {
	protected PaintManager manager;
	protected float fromX;
	protected float fromY;
	protected float toX;
	protected float toY;
	
	public ToolPencil(PaintManager manager){
		this.manager = manager;
	}
	

	@Override
	public boolean onToudhDown(MotionEvent event) {
		manager.setLock(true);
		
		fromX = manager.toModelX(event.getX());
		fromY = manager.toModelY(event.getY()-manager.getInfoHeight());
		
		Bitmap bmp = manager.getStatus().getScreen();
		if((fromX < 0) || (fromY < 0) || (bmp.getWidth() < fromX) || (bmp.getHeight() < fromY)){
			manager.setLock(false);
			return false;
		}
		
		Status status = manager.getStatus();
		status.setX(fromX);
		status.setY(fromY);
		
		manager.sendCommand(new CommandStartPencil(status));
		return true;
	}

	@Override
	public boolean onTouchUp(MotionEvent event) {
		if(manager.getLock() == false){
			return false;
		}
		
		manager.setLock(false);
		manager.replaceCommand(new CommandFinishPencil(toX, toY));
		return true;
	}

	@Override
	public boolean onTouchMove(MotionEvent event) {
		if(manager.getLock() == false){
			return false;
		}
		
		toX = manager.toModelX(event.getX());
		toY = manager.toModelY(event.getY()-manager.getInfoHeight());
		
		manager.sendCommand(new CommandPencil(toX, toY));
		return true;
	}

	@Override
	public boolean onScale(ScaleGestureDetector detector) {
		// 何もしない
		return false;
	}

	@Override
	public boolean onScaleBegin(ScaleGestureDetector detector) {
		// 何もしない
		return false;
	}

	@Override
	public boolean onScaleEnd(ScaleGestureDetector detector) {
		// 何もしない
		return false;
	}

}
